package com.zmn.oms.business.impl.conf.workturn.bizmode;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.mcc.dubbo.interfaces.dept.DeptListRemoteService;
import com.zmn.oms.business.interfaces.conf.workturn.bizmode.PlatBizModeBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.model.entity.conf.workturn.bizmode.PlatBizMode;
import com.zmn.oms.model.entity.conf.workturn.bizmode.log.PlatBizModeLog;
import com.zmn.oms.services.interfaces.conf.workturn.bizmode.PlatBizModeService;
import com.zmn.oms.services.interfaces.conf.workturn.bizmode.log.PlatBizModeLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class PlatBizModeBServiceImpl implements PlatBizModeBService {

    @Resource
    private PlatBizModeService platBizModeService;
    @Resource
    private PlatBizModeLogService platBizModeLogService;

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private DeptListRemoteService deptListRemoteService;

    @Override
    public Integer getPlatBizModel(Integer plat) {
        Objects.requireNonNull(plat);
        Integer result = platBizModeService.findByPlat(plat);
        if (NumberUtil.isNullOrZero(result)) {
            return 0;
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("Duplicates")
    public void modifyPlatBizModel(List<PlatBizMode> platBizModes, LoginStaff loginStaff) {
        Date now = DateUtil.getNow();
        String operator = loginStaff.getRealName();
        Integer deptId = loginStaff.getDeptId();
        StringBuffer stringBuffer = new StringBuffer();

        platBizModeService.deleteAllData();
        platBizModes.forEach(e -> {
            e.setCreater(operator);
            e.setCreateTime(now);
            e.setUpdater(operator);
            e.setUpdateTime(now);
            stringBuffer.append(GlobalConsts.getPlatName(e.getPlat())).append(":").append(OrderConsts.getBizModeName(e.getContractModel())).append("，");
        });

        if (CollectionUtil.isNotNullOrEmpty(platBizModes)) {
            platBizModeService.insertBatch(platBizModes);
        }

        if (stringBuffer.length() > 0) {
            PlatBizModeLog platBizModeLog = new PlatBizModeLog();
            platBizModeLog.setContent(stringBuffer.deleteCharAt(stringBuffer.length() - 1).toString());
            platBizModeLog.setCreater(operator);
            platBizModeLog.setCreateTime(now);
            platBizModeLog.setUpdater(operator);
            platBizModeLog.setUpdateTime(now);

            ResponseDTO<DeptDRO> data = deptListRemoteService.getDept(deptId);
            DeptDRO dept = new DeptDRO();
            if (data.isSuccess()) {
                dept = data.getData();
            }
            platBizModeLog.setDeptId(deptId);
            platBizModeLog.setDeptName(dept.getDeptName());
            platBizModeLogService.insert(platBizModeLog);
        }
    }
}
